﻿namespace QLPhongKham.BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDNhapKho
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _dechietkhau;
        private long _deid;
        private long _deidnhacungcap;
        private long _desophieu;
        private decimal _detongtien;
        private decimal _detongtiensauthue;
        private int _iiddangnhap;
        private int _iidkho;
        private int _iloainhap;
        private string _sghichu;
        private string _shoadon;
        private string _sngay;
        private string _sngaycapnhat;
        private string _sngayhd;
        private string _snguoigiao;
        private string _sSchema;
        private string _stenmaytinh;

        public dtoDNhapKho()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDNhapKho(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoNhapKho.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "delete from ", this._sSchema, ".", tableName, " where 1=1  and ", columnID, " = ", this._deid }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoNhapKho.cs");
                return false;
            }
        }

        private int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoNhapKho.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        private int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoNhapKho.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoNhapKho.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnSOPHIEU, ",", columnNGAY, ",", columnHOADON, ",", columnNGAYHD, ",", columnTONGTIEN, 
                    ",", columnTONGTIENSAUTHUE, ",", columnIDNHACUNGCAP, ",", columnLOAINHAP, ",", columnIDKHO, ",", columnNGUOIGIAO, ",", columnTENMAYTINH, ",", columnIDDANGNHAP, ",", columnGHICHU, 
                    ",", columnCHIETKHAU, ") values(", this._deid, ",", this._desophieu, ",to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),'", this._shoadon, "',to_timestamp('", this._sngayhd, "','dd/mm/yyyy'),:", columnTONGTIEN, ",:", columnTONGTIENSAUTHUE, 
                    ",", this._deidnhacungcap, ",", this._iloainhap, ",", this._iidkho, ",:", columnNGUOIGIAO, ",:", columnTENMAYTINH, ",", this._iiddangnhap, ",:", columnGHICHU, ",", this._dechietkhau, 
                    ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnNGUOIGIAO, NpgsqlDbType.Text, 200).Value = this._snguoigiao;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTONGTIENSAUTHUE, NpgsqlDbType.Numeric, 20).Value = this._detongtiensauthue;
                command.Parameters.Add(columnTONGTIEN, NpgsqlDbType.Numeric, 20).Value = this._detongtien;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoNhapKho.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._desophieu = 0L;
            this._sngay = "";
            this._shoadon = "";
            this._sngayhd = "";
            this._detongtien = 0M;
            this._detongtiensauthue = 0M;
            this._deidnhacungcap = 0L;
            this._iloainhap = 0;
            this._iidkho = 0;
            this._snguoigiao = "";
            this._stenmaytinh = "";
            this._sngaycapnhat = "";
            this._iiddangnhap = 0;
            this._sghichu = "";
            this._dechietkhau = 0M;
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoNhapKho.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                object obj2;
                string str = "";
                if (this._deid != 0L)
                {
                    obj2 = str;
                    str = string.Concat(new object[] { obj2, " and ", columnID, "=", this._deid });
                }
                if (this._desophieu != 0L)
                {
                    obj2 = str;
                    str = string.Concat(new object[] { obj2, " and ", columnSOPHIEU, "=", this._desophieu });
                }
                if (this._shoadon != "")
                {
                    string str3 = str;
                    str = str3 + " and " + columnHOADON + "='" + this._shoadon + "'";
                }
                if (str == "")
                {
                    str = " 1=0 ";
                }
                else
                {
                    str = " 1=1 " + str;
                }
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnSOPHIEU + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + "," + columnHOADON + ",to_char(" + columnNGAYHD + ",'dd/mm/yyyy') as " + columnNGAYHD + "," + columnTONGTIEN + "," + columnTONGTIENSAUTHUE + "," + columnIDNHACUNGCAP + "," + columnLOAINHAP + "," + columnIDKHO + "," + columnNGUOIGIAO + "," + columnTENMAYTINH + ",to_char(" + columnNGAYCAPNHAT + ",'dd/mm/yyyy hh24:mi:ss') as " + columnNGAYCAPNHAT + "," + columnIDDANGNHAP + "," + columnGHICHU + "," + columnCHIETKHAU + " from " + this._sSchema + "." + tableName + " where " + str, connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = long.Parse(dataTable.Rows[0][columnID].ToString());
                    this._desophieu = long.Parse(dataTable.Rows[0][columnSOPHIEU].ToString());
                    this._sngay = dataTable.Rows[0][columnNGAY].ToString();
                    this._shoadon = dataTable.Rows[0][columnHOADON].ToString();
                    this._sngayhd = dataTable.Rows[0][columnNGAYHD].ToString();
                    this._detongtien = decimal.Parse(dataTable.Rows[0][columnTONGTIEN].ToString());
                    this._detongtiensauthue = decimal.Parse(dataTable.Rows[0][columnTONGTIENSAUTHUE].ToString());
                    this._deidnhacungcap = long.Parse(dataTable.Rows[0][columnIDNHACUNGCAP].ToString());
                    this._iloainhap = int.Parse(dataTable.Rows[0][columnLOAINHAP].ToString());
                    this._iidkho = int.Parse(dataTable.Rows[0][columnIDKHO].ToString());
                    this._snguoigiao = dataTable.Rows[0][columnNGUOIGIAO].ToString();
                    this._stenmaytinh = dataTable.Rows[0][columnTENMAYTINH].ToString();
                    this._sngaycapnhat = dataTable.Rows[0][columnNGAYCAPNHAT].ToString();
                    this._iiddangnhap = int.Parse(dataTable.Rows[0][columnIDDANGNHAP].ToString());
                    this._sghichu = dataTable.Rows[0][columnGHICHU].ToString();
                    this._dechietkhau = decimal.Parse(dataTable.Rows[0][columnCHIETKHAU].ToString());
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoNhapKho.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoNhapKho.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnSOPHIEU + ",to_char(" + columnNGAY + ",'dd/mm/yyyy hh24:mi') as " + columnNGAY + "," + columnHOADON + ",to_char(" + columnNGAYHD + ",'dd/mm/yyyy') as " + columnNGAYHD + "," + columnTONGTIEN + "," + columnTONGTIENSAUTHUE + "," + columnIDNHACUNGCAP + "," + columnLOAINHAP + "," + columnIDKHO + "," + columnNGUOIGIAO + "," + columnTENMAYTINH + "," + columnIDDANGNHAP + "," + columnGHICHU + "," + columnCHIETKHAU + " from " + this._sSchema + "." + tableName + " order by " + columnNGAY + " desc", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoNhapKho.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoNhapKho.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnSOPHIEU, " = ", this._desophieu, ",", columnNGAY, " = to_timestamp('", this._sngay, "','dd/mm/yyyy hh24:mi'),", columnHOADON, " = '", this._shoadon, 
                    "',", columnNGAYHD, " = to_timestamp('", this._sngayhd, "','dd/mm/yyyy'),", columnTONGTIEN, " = :", columnTONGTIEN, ",", columnTONGTIENSAUTHUE, " = :", columnTONGTIENSAUTHUE, ",", columnIDNHACUNGCAP, " = ", this._deidnhacungcap, 
                    ",", columnLOAINHAP, " = ", this._iloainhap, ",", columnIDKHO, " = ", this._iidkho, ",", columnNGUOIGIAO, " = :", columnNGUOIGIAO, ",", columnTENMAYTINH, " = :", columnTENMAYTINH, 
                    ",", columnNGAYCAPNHAT, " = now(),", columnIDDANGNHAP, " = ", this._iiddangnhap, ",", columnGHICHU, " = :", columnGHICHU, ",", columnCHIETKHAU, "=:", columnCHIETKHAU, " where ", columnID, 
                    " = ", this._deid
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnNGUOIGIAO, NpgsqlDbType.Text, 200).Value = this._snguoigiao;
                command.Parameters.Add(columnTENMAYTINH, NpgsqlDbType.Text, 200).Value = this._stenmaytinh;
                command.Parameters.Add(columnGHICHU, NpgsqlDbType.Text, 200).Value = this._sghichu;
                command.Parameters.Add(columnTONGTIENSAUTHUE, NpgsqlDbType.Numeric, 20).Value = this._detongtiensauthue;
                command.Parameters.Add(columnTONGTIEN, NpgsqlDbType.Numeric, 20).Value = this._detongtien;
                command.Parameters.Add(columnCHIETKHAU, NpgsqlDbType.Numeric, 20).Value = this._dechietkhau;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoNhapKho.cs");
                return false;
            }
        }

        public static string columnCHIETKHAU
        {
            get
            {
                return "chietkhau";
            }
        }

        public static string columnGHICHU
        {
            get
            {
                return "ghichu";
            }
        }

        public static string columnHOADON
        {
            get
            {
                return "hoadon";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDANGNHAP
        {
            get
            {
                return "iddangnhap";
            }
        }

        public static string columnIDKHO
        {
            get
            {
                return "idkho";
            }
        }

        public static string columnIDNHACUNGCAP
        {
            get
            {
                return "idnhacungcap";
            }
        }

        public static string columnLOAINHAP
        {
            get
            {
                return "loainhap";
            }
        }

        public static string columnNGAY
        {
            get
            {
                return "ngay";
            }
        }

        public static string columnNGAYCAPNHAT
        {
            get
            {
                return "ngaycapnhat";
            }
        }

        public static string columnNGAYHD
        {
            get
            {
                return "ngayhd";
            }
        }

        public static string columnNGUOIGIAO
        {
            get
            {
                return "nguoigiao";
            }
        }

        public static string columnSOPHIEU
        {
            get
            {
                return "sophieu";
            }
        }

        public static string columnTENMAYTINH
        {
            get
            {
                return "tenmaytinh";
            }
        }

        public static string columnTONGTIEN
        {
            get
            {
                return "tongtien";
            }
        }

        public static string columnTONGTIENSAUTHUE
        {
            get
            {
                return "tongtiensauthue";
            }
        }

        private string NGAYCAPNHAT
        {
            get
            {
                return this._sngaycapnhat;
            }
            set
            {
                this._sngaycapnhat = value;
            }
        }

        public decimal pCHIETKHAU
        {
            get
            {
                return this._dechietkhau;
            }
            set
            {
                this._dechietkhau = value;
            }
        }

        public string pGHICHU
        {
            get
            {
                return this._sghichu;
            }
            set
            {
                this._sghichu = value;
            }
        }

        public string pHOADON
        {
            get
            {
                return this._shoadon;
            }
            set
            {
                this._shoadon = value;
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDANGNHAP
        {
            get
            {
                return this._iiddangnhap;
            }
            set
            {
                this._iiddangnhap = value;
            }
        }

        public int pIDKHO
        {
            get
            {
                return this._iidkho;
            }
            set
            {
                this._iidkho = value;
            }
        }

        public long pIDNHACUNGCAP
        {
            get
            {
                return this._deidnhacungcap;
            }
            set
            {
                this._deidnhacungcap = value;
            }
        }

        public int pLOAINHAP
        {
            get
            {
                return this._iloainhap;
            }
            set
            {
                this._iloainhap = value;
            }
        }

        public string pNGAY
        {
            get
            {
                return this._sngay;
            }
            set
            {
                this._sngay = value;
            }
        }

        public string pNGAYHD
        {
            get
            {
                return this._sngayhd;
            }
            set
            {
                this._sngayhd = value;
            }
        }

        public string pNGUOIGIAO
        {
            get
            {
                return this._snguoigiao;
            }
            set
            {
                this._snguoigiao = value;
            }
        }

        public long pSOPHIEU
        {
            get
            {
                return this._desophieu;
            }
            set
            {
                this._desophieu = value;
            }
        }

        public string pTENMAYTINH
        {
            get
            {
                return this._stenmaytinh;
            }
            set
            {
                this._stenmaytinh = value;
            }
        }

        public decimal pTONGTIEN
        {
            get
            {
                return this._detongtien;
            }
            set
            {
                this._detongtien = value;
            }
        }

        public decimal pTONGTIENSAUTHUE
        {
            get
            {
                return this._detongtiensauthue;
            }
            set
            {
                this._detongtiensauthue = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_nhapkho";
            }
        }
    }
}

